#!/bin/bash
#
# First, this script will look for a manifest to write to the ManagedInstalls
# plist in the first argument - $1. 
#
# Next, it looks for /tmp/ManagedInstalls.plist, and if found, will deploy the
# settings found in that file to the target machine.
#
# Finally, if both of the other options have failed to produce a manifest, it
# will use the Munki Image Selector.app to ask the user for the manifest.
#
plist="/Volumes/${DS_LAST_SELECTED_TARGET}/Library/Preferences/ManagedInstalls"
cacheFile="/private/var/tmp/${DS_PRIMARY_MAC_ADDRESS}"


# grab the manifest from the argument list.
SelectedManifest="${1}"


#...if that failed, grab the manifest from the cached settings file.
if [ "${SelectedManifest}" = "" ]; then
	if [ -e "${cacheFile}.plist" ]; then
		SelectedManifest="`defaults read \"${cacheFile}\" ClientIdentifier`"
	fi
fi


#...if that failed, ask the user for the manifest using Munki Image Selector.app
if [ "${SelectedManifest}" = "" ]; then
  BASE_DIR=`dirname "${0}"`
  SelectedManifest=`"${BASE_DIR}/Munki Image Selector.app/Contents/MacOS/Munki Image Selector"`
  SelectedManifest="${SelectedManifest}/generic"
  echo "${SelectedManifest}"
fi

# Checking for the target path to be sure we're not installing on an empty hard
# drive.
if [ ! -e "`dirname \"${plist}.plist\"`" ]; then
  echo "INVALID PLIST PATH '${plist}.plist'"
  exit 1
fi


# Write the settings to the target hard drive.
echo "Using munki group '${SelectedManifest}'"
defaults write "${plist}" SoftwareRepoURL "http://apple-prime.cache.denison.edu"
defaults write "${plist}" SoftwareUpdateServerURL "http://reposado.cc.denison.edu/content/catalogs/others/index-mountainlion-lion-snowleopard-leopard.merged-1_production.sucatalog"
defaults write "${plist}" ClientIdentifier -string "${SelectedManifest}"
defaults write "${plist}" InstallAppleSoftwareUpdates -bool YES
defaults write "${plist}" DaysBetweenNotifications -int 0
defaults write "${plist}" ShowRemovalDetail -bool YES

exit 0
